package org.icress.fusion.b2b

class ProductCategoryController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        render view : '/b2b/productCategory/list', model : [productCategoryInstanceList: ProductCategory.list(params), productCategoryInstanceTotal: ProductCategory.count()]
    }

    def create = {
        def productCategoryInstance = new ProductCategory()
        productCategoryInstance.properties = params
        render view : '/b2b/productCategory/create', model :  [productCategoryInstance: productCategoryInstance]
    }

    def save = {
        def productCategoryInstance = new ProductCategory(params)
        if (!productCategoryInstance.hasErrors() && productCategoryInstance.save()) {
            flash.message = "productCategory.created"
            flash.args = [productCategoryInstance.id]
            flash.defaultMessage = "ProductCategory ${productCategoryInstance.id} created"
            redirect(action: "show", id: productCategoryInstance.id)
        }
        else {
            render(view: "/b2b/productCategory/create", model: [productCategoryInstance: productCategoryInstance])
        }
    }

    def show = {
        def productCategoryInstance = ProductCategory.get(params.id)
        if (!productCategoryInstance) {
            flash.message = "productCategory.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "ProductCategory not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/productCategory/show', model : [productCategoryInstance: productCategoryInstance]
        }
    }

    def edit = {
        def productCategoryInstance = ProductCategory.get(params.id)
        if (!productCategoryInstance) {
            flash.message = "productCategory.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "ProductCategory not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/productCategory/edit', model :  [productCategoryInstance: productCategoryInstance]
        }
    }

    def update = {
        def productCategoryInstance = ProductCategory.get(params.id)
        if (productCategoryInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (productCategoryInstance.version > version) {
                    
                    productCategoryInstance.errors.rejectValue("version", "productCategory.optimistic.locking.failure", "Another user has updated this ProductCategory while you were editing")
                    render(view: "/b2b/productCategory/edit", model: [productCategoryInstance: productCategoryInstance])
                    return
                }
            }
            productCategoryInstance.properties = params
            if (!productCategoryInstance.hasErrors() && productCategoryInstance.save()) {
                flash.message = "productCategory.updated"
                flash.args = [params.id]
                flash.defaultMessage = "ProductCategory ${params.id} updated"
                redirect(action: "show", id: productCategoryInstance.id)
            }
            else {
                render(view: "/b2b/productCategory/edit", model: [productCategoryInstance: productCategoryInstance])
            }
        }
        else {
            flash.message = "productCategory.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "ProductCategory not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def productCategoryInstance = ProductCategory.get(params.id)
        if (productCategoryInstance) {
            try {
                productCategoryInstance.delete()
                flash.message = "productCategory.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "ProductCategory ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "productCategory.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "ProductCategory ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "productCategory.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "ProductCategory not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
